<template>
	<view>
	<!-- #ifdef MP-WEIXIN -->
	<btnlogin :zheshow='zheshow' />
	<!-- #endif -->
	<!-- #ifdef APP || H5 -->
	<view class="login">
		<view class="content">
			<!-- 头部logo -->
			<view class="header">
				<image :src="logoImage"></image>
			</view>
			<!-- 主体表单 -->
			<view class="main">
				<wInput
					v-model="phoneData"
					type="text"
					maxlength="11"
					placeholder="用户名/电话"
					:focus="isFocus"
				></wInput>
				<wInput
					v-model="passData"
					type="password"
					maxlength="11"
					placeholder="密码"
				></wInput>
			</view>
			<wButton 
				class="wbutton"
				text="登 录"
				:rotate="isRotate" 
				@click="startLogin"
			></wButton>
			
			<!-- 其他登录 -->
			<view class="other_login cuIcon">
				<view class="login_icon">
					<view class="cuIcon-weixin" @tap="login_weixin"></view>
				</view>
				<view class="login_icon">
					<view class="cuIcon-weibo" @tap="login_weibo"></view>
				</view>
				<view class="login_icon">
					<view class="cuIcon-github" @tap="login_github"></view>
				</view>
			</view>
			
			<!-- 底部信息 -->
			<view class="footer">
				<navigator url="forget" open-type="navigate">找回密码</navigator>
				<text>|</text>
				<navigator url="register" open-type="navigate">注册账号</navigator>
			</view>
		</view>
	</view>
	<!-- #endif -->
	</view>
</template>

<script>
	let _this;
	const api = require('@/config/api');
	const util = require('@/utils/util');
	import wInput from '@/components/watch-login/watch-input.vue' //input
	import wButton from '@/components/watch-login/watch-button.vue' //button
	import btnlogin from '@/components//watch-login/butlogin';
	export default {
		data() {
			return {
				zheshow:false,
				//logo图片 base64
				logoImage: '',
				phoneData:'', //用户/电话
				passData:'', //密码
				isRotate: false, //是否加载旋转
				isFocus: true // 是否聚焦
			};
		},
		components:{
			wInput,
			wButton,
			btnlogin,
		},

		mounted() {
			_this= this;
			// #ifdef MP-WEIXIN
			let is_new_login = uni.getStorageSync('is_new_login');
			if(is_new_login != null && is_new_login != '' && typeof(is_new_login) != "undefined")
			{
				this.zheshow = !this.zheshow;
			}else{
				if(util.Newlogin()){
					let tourl = uni.getStorageSync('tourl');
					if(tourl){
						uni.setStorageSync('tourl','');
						uni.redirectTo({
							url: tourl
						});
					}else{
						uni.navigateBack({
							delta: 1
						});
						// uni.switchTab({
						// 	url: '/pages/tabbar/center'
						// });
					}
				}else{
					this.zheshow = !this.zheshow
				}
			}
			// #endif
			//this.isLogin();
		},
		methods: {
			loset(Logon){
				let is_new_login = uni.getStorageSync('is_new_login');
				util.request(api.loginUsersInfo, {
					nickname: Logon.nickname,
					active: Logon.active,
					key: is_new_login
				}, 'POST').then(function(res) {
					
					if (res.error == 500006) {
						uni.showToast({title:'登陆出错',icon:'error'})
					} else  {
						uni.setStorageSync('is_new_login', null);
						uni.setStorageSync('user_info', res.data.info);
						uni.setStorageSync('user_token', res.data.token);
						let tourl = uni.getStorageSync('tourl');
						if(tourl){
							uni.setStorageSync('tourl','');
							uni.redirectTo({
								url: tourl
							});
						}else{
							uni.navigateBack({
								delta: 1
							});
							// uni.switchTab({
							// 	url: '/pages/tabbar/index'
							// });
						}
					}
					

				});

			},
			isLogin(){
				//判断缓存中是否登录过，直接登录
				// try {
				// 	const value = uni.getStorageSync('setUserData');
				// 	if (value) {
				// 		//有登录信息
				// 		console.log("已登录用户：",value);
				// 		_this.$store.dispatch("setUserData",value); //存入状态
				// 		uni.reLaunch({
				// 			url: '../../../pages/index',
				// 		});
				// 	}
				// } catch (e) {
				// 	// error
				// }
			},
		    startLogin(e){
                console.log(e)
				//登录
				if(this.isRotate){
					//判断是否加载中，避免重复点击请求
					return false;
				}
				if (this.phoneData.length == "") {
				     uni.showToast({
				        icon: 'none',
						position: 'bottom',
				        title: '用户名不能为空'
				    });
				    return;
				}
		        if (this.passData.length < 5) {
		            uni.showToast({
		                icon: 'none',
						position: 'bottom',
		                title: '密码不正确'
		            });
		            return;
		        }
				
				console.log("登录成功")
				
				_this.isRotate=true
				setTimeout(function(){
					_this.isRotate=false
				},3000)
				// uni.showLoading({
				// 	title: '登录中'
				// });
				// getLogin()
				// .then(res => {
				// 	//console.log(res)
				// 	//简单验证下登录（不安全）
				// 	if(_this.phoneData==res.data.username && _this.passData==res.data.password){
				// 		let userdata={
				// 			"username":res.data.username,
				// 			"nickname":res.data.nickname,
				// 			"accesstoken":res.data.accesstoken,
				// 		} //保存用户信息和accesstoken
				// 		_this.$store.dispatch("setUserData",userdata); //存入状态
				// 		try {
				// 			uni.setStorageSync('setUserData', userdata); //存入缓存
				// 		} catch (e) {
				// 			// error
				// 		}
				// 		uni.showToast({
				// 			icon: 'success',
				// 			position: 'bottom',
				// 			title: '登录成功'
				// 		});
				// 		uni.reLaunch({
				// 			url: '../../../pages/index',
				// 		});
				// 	}else{
				// 		_this.passData=""
				// 		uni.showToast({
				// 			icon: 'error',
				// 			position: 'bottom',
				// 			title: '账号或密码错误，账号admin密码admin'
				// 		});
				// 	}
				// 	uni.hideLoading();
				// }).catch(err => {
				// 	uni.hideLoading();
				// })
				
		    },
			login_weixin() {
				//微信登录
				uni.showToast({
					icon: 'none',
					position: 'bottom',
					title: '...'
				});
			},
			login_weibo() {
				//微博登录
				uni.showToast({
					icon: 'none',
					position: 'bottom',
					title: '...'
				});
			},
			login_github() {
				//github登录
				uni.showToast({
					icon: 'none',
					position: 'bottom',
					title: '...'
				});
			}
		}
	}
</script>

<style>
	@import url("../../components/watch-login/css/icon.css");
	@import url("./css/main.css");
</style>
